package com.ccl.iot.publics;import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import java.util.ArrayList;
import java.util.List;

public class NetInfo {
	public enum TWIFI_AUTH_MODE{
		;
		public static final byte EWIFI_AUTH_MODE_UNKNOWN = -1;
		public static final byte EWIFI_AUTH_MODE_OPEN = 0x00;
		public static final byte EWIFI_AUTH_MODE_SHARED = 0x01;
		public static final byte EWIFI_AUTH_MODE_AUTOSWITCH = 0x02;
		public static final byte EWIFI_AUTH_MODE_WPA = 0x03;
		public static final byte EWIFI_AUTH_MODE_WPAPSK = 0x04;
		public static final byte EWIFI_AUTH_MODE_WPANONE = 0x05;
		public static final byte EWIFI_AUTH_MODE_WPA2 = 0x06;
		public static final byte EWIFI_AUTH_MODE_WPA2PSK = 0x07;   
		public static final byte EWIFI_AUTH_MODE_WPA1WPA2 = 0x08;
		public static final byte EWIFI_AUTH_MODE_WPA1PSKWPA2PSK = 0x09;
	}
	
	public static class WIFIAuthorizedInfo{
		
		public String iSSID = null;
		public String iPSK = null;
		public int iType = 0;
		public byte iAuthMode = TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_OPEN;
	}
	
   public static ArrayList<WIFIAuthorizedInfo> GetWifiAuthorizedInfo(){
	   byte tData[] = FileSystem.ReadFile("/data/misc/wifi/wpa_supplicant.conf", true);
   
	   if (tData != null){
		   String tInfoString = new String(tData);
		   String tInfoStrings[] = tInfoString.split("\n");
	   
		   if (tInfoStrings != null){
			   WIFIAuthorizedInfo tInfo = null;
			   ArrayList<WIFIAuthorizedInfo> tInfos = new ArrayList<WIFIAuthorizedInfo>();
			   
			   for (int i = 0; i < tInfoStrings.length; i++){
				   tInfoString = tInfoStrings[i].trim();
				   
				   if (tInfoString.compareToIgnoreCase("network={") == 0){
					   if (tInfo != null){
						   tInfos.add(tInfo);
					   }
					   
					   tInfo = new WIFIAuthorizedInfo();
				   }else if (tInfoString.compareTo("}") == 0){
					   if (tInfo != null){
						   tInfos.add(tInfo);
						   
						   tInfo = null;
					   }
				   }else if (tInfo != null){
					   if (tInfoString.startsWith("ssid=")){
						   tInfo.iSSID = tInfoString.replace("\"", "").substring(5);
					   }else if (tInfoString.startsWith("psk=")){
						   tInfo.iPSK = tInfoString.replace("\"", "").substring(4);						   
					   }else if (tInfoString.startsWith("wep_key")){
						   tInfo.iPSK = tInfoString.substring(9);						   
					   }else if (tInfoString.startsWith("key_mgmt=")){
						   String tAuthMode = tInfoString.substring(9);
						   
						   if (tAuthMode != null){
							   if (tAuthMode.compareToIgnoreCase("NONE") == 0){
								   tInfo.iAuthMode = TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_OPEN;
							   }else if (tAuthMode.compareToIgnoreCase("OPEN_SHARED") == 0){
								   tInfo.iAuthMode = TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_SHARED;
							   }else if (tAuthMode.compareToIgnoreCase("WPA") == 0){
								   tInfo.iAuthMode = TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_WPA;
							   }else if (tAuthMode.compareToIgnoreCase("WPA-PSK") == 0){
								   tInfo.iAuthMode = TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_WPAPSK;
							   }else{
								   tInfo.iAuthMode = TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_AUTOSWITCH;
							   }
						   }
					   }
				   }
			   }
			   
			   if (tInfos.size() > 0){
				   return tInfos;
			   }
		   }
	   }
	   
	   return null;
   }
   
   public static NetworkInfo GetActiveNetwokInfo(Context aContext){
	   if (aContext != null){
		   ConnectivityManager tCM = (ConnectivityManager) aContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		   
		   if (tCM != null){
			   return tCM.getActiveNetworkInfo();
		   }
	   }
	   
	   return null;
   }
   
   public static WifiInfo GetActiveWifiInfo(Context aContext){
	   if (aContext != null){
		   WifiManager tWifi = (WifiManager) aContext.getSystemService (Context.WIFI_SERVICE);
		   
		   if (tWifi != null){
			   if (tWifi.isWifiEnabled()){
				   return tWifi.getConnectionInfo();
			   }
		   }
	   }
	   
	   return null;
   }
   
   public static String GetWifiSSID(WifiInfo aInfo){
	   if (aInfo != null){
		   String tSSID = aInfo.getSSID();
		   
		   if (tSSID != null){
			   return tSSID.replace("\"", "");
		   }
	   }
	   
	   return null;
   }
   
   public static String GetActiveWifiSSID(Context aContext){
	   return GetWifiSSID(GetActiveWifiInfo(aContext));
   }
   
   public static byte GetActiveWifiAuthMode(Context aContext){
	   if (aContext != null){
		   WifiManager tWifi = (WifiManager) aContext.getSystemService (Context.WIFI_SERVICE);
		   
		   if (tWifi != null){
			   if (tWifi.isWifiEnabled()){
				   String tSSID = GetWifiSSID(tWifi.getConnectionInfo());
				   
				   if (tSSID != null){
					   List<ScanResult> tResults = tWifi.getScanResults();
					   
					   if (tResults != null){
						   for (int i = 0; i < tResults.size(); i++){
							   ScanResult tResult = tResults.get(i);
							   
							   if (tResult != null && tResult.SSID.equals(tSSID)){
									boolean tWpaPsk = tResult.capabilities.contains("WPA-PSK");
						        	boolean tWpa2Psk = tResult.capabilities.contains("WPA2-PSK");
									boolean tWpa = tResult.capabilities.contains("WPA-EAP");
						        	boolean tWpa2 = tResult.capabilities.contains("WPA2-EAP");
									
									if (tResult.capabilities.contains("WEP")){
										return TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_SHARED;
									}
									
									if (tWpaPsk && tWpa2Psk){
										return TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_WPA1PSKWPA2PSK;
									}else if (tWpa2Psk){
										return TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_WPA2PSK;
									}else if (tWpaPsk){
										return TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_WPAPSK;
									}

									if (tWpa && tWpa2){
										return TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_WPA1WPA2;
									}else if (tWpa2){
										return TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_WPA2;
									}else if (tWpa){
										return TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_WPA;
									}else{
										return TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_OPEN;
									}
							   }
						   }
					   }
				   }
			   }
		   }
	   }
	   
	   return TWIFI_AUTH_MODE.EWIFI_AUTH_MODE_UNKNOWN;
   }
}
